package cn.itcast.controller;

import cn.itcast.entity.User;
import cn.itcast.feign.FeignClient2;
import cn.itcast.feign.UserFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
public class MovieController {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${user.userServicePath}")
    private String userServicePath;

    @Autowired
    @Qualifier("car-manager-user")
    private UserFeignClient feignClient;


    @Autowired
    @Qualifier("feignClient2")
    private FeignClient2 feignClient2;

    /**
     * @param serviceName
     * @return http://localhost:8764/eureka/apps/{serviceName}
     */
    @GetMapping("/{serviceName}")
    public String findServiceInfoFromEurekaByServiceName(@PathVariable String serviceName) {
        return this.feignClient2.findServiceInfoFromEurekaByServiceName(serviceName);
    }

    /**
     * @param id
     * @return http://car-manager-user/simple/{id}
     */
    @GetMapping("/get/{id}")
    public User getfeign(@PathVariable("id") Long id) {
        return feignClient.get(id);
    }

    @GetMapping("/movie/{id}")
    public User findById(@PathVariable Long id) {
        //返回指定路径的结果
        return this.restTemplate.getForObject(this.userServicePath + id, User.class);
    }

    @GetMapping("/simple/{id}")
    public User getbyId(@PathVariable Long id) {
        return this.restTemplate.getForObject(this.userServicePath + id, User.class);
    }

    @GetMapping("/test/{id}")
    public String test(@PathVariable Long id) {
        return "this is" + id;
    }
}
